﻿package mortal.game.scene3D.map3D
{
    import com.fyGame.fyMap.*;
    import com.gengine.debug.*;
    import com.gengine.resource.loader.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import mortal.game.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.util.*;

    public class MapLoader extends EventDispatcher
    {
        private var _maploader:MapDataLoader;
        private var _sceneloader:URLLoader;
        private var _isMaploaded:Boolean = false;
        private var _isMapLoading:Boolean = false;
        private var _isSceneLoaded:Boolean = false;
        private var _mapDic:Dictionary;
        private var _sceneDic:Dictionary;
        private static var _instance:MapLoader = new MapLoader;

        public function MapLoader()
        {
            this._mapDic = new Dictionary();
            this._sceneDic = new Dictionary();
            return;
        }// end function

        public function LoadSceneInfo() : void
        {
            return;
        }// end function

        public function load() : void
        {
            var _loc_1:* = this._mapDic[MapFileUtil.mapID];
            if (_loc_1 == null)
            {
                if (this._maploader)
                {
                    this._maploader.removeEventListener(Event.COMPLETE, this.onMapLoadedHandler);
                }
                this._maploader = new MapDataLoader();
                this._isMaploaded = false;
                this._isMapLoading = true;
                this._maploader.addEventListener(Event.COMPLETE, this.onMapLoadedHandler);
                this._maploader.load(MapFileUtil.mapDataPath, null);
            }
            else
            {
                Game.mapInfo = _loc_1;
                this._isMaploaded = true;
                this.onCompleteHandler();
            }
            return;
        }// end function

        private function onMapLoadedHandler(event:Event) : void
        {
            var _loc_2:* = event.target as MapDataLoader;
            _loc_2.removeEventListener(Event.COMPLETE, this.onMapLoadedHandler);
            Log.debug("服务器地图ID:" + MapFileUtil.mapID);
            Log.debug("配置文件地图ID:" + _loc_2.mapInfo.mapId);
            if (_loc_2.mapInfo.mapId != MapFileUtil.mapID)
            {
                ThrowError.show("地图ID:服务器ID:" + MapFileUtil.mapID + "|配置数据ID:" + _loc_2.mapInfo.mapId);
                if (_loc_2.mapInfo.mapId == 0)
                {
                    _loc_2.mapInfo.mapId = MapFileUtil.mapID;
                }
            }
            Game.mapInfo = this._maploader.mapInfo;
            this._mapDic[MapFileUtil.mapID] = _loc_2.mapInfo;
            this._isMaploaded = true;
            this._isMapLoading = false;
            this.onCompleteHandler();
            return;
        }// end function

        public function loadMapData(param1:int, param2:Function = null) : void
        {
            var onMapIdLoadedHandler:Function;
            var _mapIdLoader:MapDataLoader;
            var mapId:* = param1;
            var callBack:* = param2;
            onMapIdLoadedHandler = function (event:Event) : void
            {
                var _loc_2:* = event.target as MapDataLoader;
                _loc_2.removeEventListener(Event.COMPLETE, onMapIdLoadedHandler);
                _mapDic[mapId] = _loc_2.mapInfo;
                if (callBack != null)
                {
                    callBack(_loc_2.mapInfo);
                }
                return;
            }// end function
            ;
            var mapInfo:* = this._mapDic[mapId];
            if (mapInfo == null)
            {
                _mapIdLoader = new MapDataLoader();
                _mapIdLoader.addEventListener(Event.COMPLETE, onMapIdLoadedHandler);
                _mapIdLoader.load(MapFileUtil.getMapDataPathByMapId(mapId), null);
            }
            else if (callBack != null)
            {
                this.callBack(mapInfo);
            }
            return;
        }// end function

        public function getMapData(param1:int) : FyMapInfo
        {
            return this._mapDic[param1] as FyMapInfo;
        }// end function

        private function onIOErrorHandler(event:ErrorEvent) : void
        {
            Log.system("MapLoader:" + event.text);
            return;
        }// end function

        public function stop() : void
        {
            if (this._maploader)
            {
                this._maploader.removeEventListener(Event.COMPLETE, this.onMapLoadedHandler);
                this._maploader.stop();
            }
            this._isMaploaded = false;
            this._isMapLoading = false;
            return;
        }// end function

        private function onCompleteHandler() : void
        {
            Game.sceneInfo = SceneConfig.instance.getSceneInfo(MapFileUtil.mapID);
            dispatchEvent(new Event(Event.COMPLETE));
            return;
        }// end function

        public static function get instance() : MapLoader
        {
            return _instance;
        }// end function

    }
}
